﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using agsXMPP;

namespace hellaBot
{
    class Program
    {
        private static string errorstring = "";
        private static bool dienow = false;
        private static Thread termThread;
        public static Config configuration;

        public static void Die(string error)
        {
            errorstring = error;
            dienow = true;
        }

        public static void Die()
        {
            dienow = true;
        }

        static void Main(string[] args)
        {
            // Read in the config
            try
            {
                configuration.mucs = new List<Jid>();
                configuration.users = new List<Jid>();
                string[] conffile = System.IO.File.ReadAllLines("./hellabot.conf");
                foreach (string line in conffile)
                {
                    char[] splitter = { ' ' };
                    string[] parts = line.Split(splitter); // Break into parts
                    switch (parts[0])
                    {
                        case "server":
                            configuration.server = parts[1];
                            break;
                        case "username":
                            configuration.user = parts[1];
                            break;
                        case "password":
                            configuration.password = parts[1];
                            break;
                        case "downloadpath":
                            configuration.downloadFolder = parts[1];
                            break;
                        case "hellanzbxmlurl":
                            configuration.hellanzbxmlurl = parts[1];
                            break;
                        case "hellanzbpass":
                            configuration.hellanzbpass = parts[1];
                            break;
                        case "users":
                            for (int i = 1; i < parts.Length; i++) // Purposely ignore parts[0] which is 'users'
                            {
                                configuration.users.Add(new Jid(parts[i]));
                            }
                            break;
                        case "chatrooms":
                            for (int i = 1; i < parts.Length; i++) // Purposely ignore parts[0] which is 'users'
                            {
                                configuration.mucs.Add(new Jid(parts[i]));
                            }
                            break;                    
                    }                    
                }
                if (configuration.server == null || configuration.user == null || configuration.password == null || configuration.downloadFolder == null)
                {
                    Terminal.PrintError("Configuration does not contain required directives (server, username, password, downloadpath). Try again - Press any key to exit");
                    Console.ReadKey();
                    Environment.Exit(1);
                }
            }
            catch (Exception e)
            {
                Terminal.PrintError("Unable to open config file - Press any key to exit");
                Console.ReadKey();
                Environment.Exit(1);
            }

            // Create jabber object and attempt login
            Jabber xmpp = new Jabber(configuration.server);            
            try 
            {   
                xmpp.Login(configuration.user, configuration.password, "hellaBot");
            }
            catch
            {
                Terminal.PrintError("Unable to login to jabber server, cannot proceed");
                return;
            }

            // Spawn a thread to handle reading input from the terminal
            Terminal.xmpp = xmpp;
            termThread = new Thread(new ThreadStart(Terminal.ReadInput));
            termThread.Start();

            // Spin while life goes on around us unless something somewhere sets dienow
            while (!dienow)
            {
                System.Threading.Thread.Sleep(1000);
            }
            if (dienow)
            {
                termThread.Abort();
                if (errorstring != "")
                {
                    Terminal.PrintError(errorstring);
                    Terminal.PrintMessage("Press any key to exit");
                    Console.ReadKey();
                }
                // This is required to kill off the app with the termthread blocking on user input :/ (probably a better way than this)
                Environment.Exit(1);
                return;
                
            }
        }
    }

    struct Config
    {
        public string server, user, password, downloadFolder, hellanzbxmlurl, hellanzbpass;
        public List<Jid> mucs, users;
    }
}
